﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class DETAI_Form : Form
    {

        THAMSO thamso = new THAMSO();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        LOAIDETAI_Data loaidetai_Data = new LOAIDETAI_Data();
        DETAI detai = new DETAI();
        DETAI_Data detai_Data = new DETAI_Data();

        public DETAI_Form()
        {
            InitializeComponent();
        }

        private void DETAI_Form_Load(object sender, EventArgs e)
        {
            txtHocKy.Text = "";
            txtMaDeTai.Text = "";
            txtNamHoc.Text= DateTime.Now.Year.ToString();
            txtSoTietChuan.Text = "";
            txtTenDeTai.Text = "";
            cbGiaoVien.Items.Clear();
            cbLoaiDeTai.Items.Clear();

            loaidetai_Data.loaidetai_Combobox(cbLoaiDeTai);
            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            txtHocKy.Text = "";
            txtMaDeTai.Text = "DT";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtSoTietChuan.Text = "";
            txtTenDeTai.Text = "";
            cbGiaoVien.Items.Clear();
            cbLoaiDeTai.Items.Clear();

            loaidetai_Data.loaidetai_Combobox(cbLoaiDeTai);
            giaovien_Data.giaovien_Combobox(cbGiaoVien);
            detai.hienthi_DeTai(dtgDeTai);
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaDeTai.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma De Tai");
                txtMaDeTai.Focus();
                return;
            }
            else if (txtTenDeTai.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ten De Tai");
                txtTenDeTai.Focus();
                return;
            }
            else if (txtHocKy.Text == "")
            {
                MessageBox.Show("Ban chua nhap hoc ki");
                txtHocKy.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (txtSoTietChuan.Text == "")
            {
                MessageBox.Show("Ban chua nhap So Tiet chuan");
                txtSoTietChuan.Focus();
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("ban chua chuon Giao Vien");
                return;
            }
            else if (cbLoaiDeTai.Text == "")
            {
                MessageBox.Show("Ban chua chon loai detai");
                return;
            }

            else if (detai_Data.kiemtra_MaDeTai(txtMaDeTai.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ma De Tai");
                txtMaDeTai.Focus();
                return;
            }
            else if (detai_Data.kiemtra_TenDeTai(txtTenDeTai.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ma De Tai");
                txtTenDeTai.Focus();
                return;
            }
            else
            {
                try
                {
                    if (Convert.ToInt16(txtHocKy.Text) < 0 || Convert.ToInt16(txtHocKy.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung hoc ki");
                        txtHocKy.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu hoc ki \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtNamHoc.Text) < 0 || Convert.ToInt16(txtNamHoc.Text) > DateTime.Now.Year + 5)
                    {
                        MessageBox.Show("Ban chua nhap dung nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu nam hoc \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtSoTietChuan.Text) < 0 || Convert.ToInt16(txtSoTietChuan.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung so tiet chuan");
                        txtSoTietChuan.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung so tiet chuan \n" + ex.Message);
                }
                detai.maDT = txtMaDeTai.Text.ToString();
                detai.tenDT = txtTenDeTai.Text.ToString();
                detai.loaiDT = cbLoaiDeTai.Text.ToString();
                detai.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                detai.namhoc = Convert.ToInt16(txtNamHoc.Text);
                detai.hocki = Convert.ToInt16(txtHocKy.Text);
                detai.sotietchuan = Convert.ToInt16(txtSoTietChuan.Text);

                detai.them_DeTai(detai);
                detai.hienthi_DeTai(dtgDeTai);

                txtHocKy.Text = "";
                txtMaDeTai.Text = "DT";
                txtNamHoc.Text = DateTime.Now.Year.ToString();
                txtSoTietChuan.Text = "";
                txtTenDeTai.Text = "";
                cbGiaoVien.Items.Clear();
                cbLoaiDeTai.Items.Clear();

                loaidetai_Data.loaidetai_Combobox(cbLoaiDeTai);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }
            

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaDeTai.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma De Tai");
                txtMaDeTai.Focus();
                return;
            }
            else if (txtTenDeTai.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ten De Tai");
                txtTenDeTai.Focus();
                return;
            }
            else if (txtHocKy.Text == "")
            {
                MessageBox.Show("Ban chua nhap hoc ki");
                txtHocKy.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (txtSoTietChuan.Text == "")
            {
                MessageBox.Show("Ban chua nhap So Tiet chuan");
                txtSoTietChuan.Focus();
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("ban chua chuon Giao Vien");
                return;
            }
            else if (cbLoaiDeTai.Text == "")
            {
                MessageBox.Show("Ban chua chon loai detai");
                return;
            }
            else
            {
                try
                {
                    if (Convert.ToInt16(txtHocKy.Text) < 0 || Convert.ToInt16(txtHocKy.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung hoc ki");
                        txtHocKy.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu hoc ki\n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtNamHoc.Text) < 1900 || Convert.ToInt16(txtNamHoc.Text) >DateTime.Now.Year + 5)
                    {
                        MessageBox.Show("Ban chua nhap dung nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu nam hoc \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtSoTietChuan.Text) < 0 || Convert.ToInt16(txtSoTietChuan.Text) > 100)
                    {
                        MessageBox.Show("Ban chua nhap dung so tiet chuan");
                        txtSoTietChuan.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung so tiet chuan\n" + ex.Message);
                }
                detai.maDT = txtMaDeTai.Text.ToString();
                detai.tenDT = txtTenDeTai.Text.ToString();
                detai.loaiDT = cbLoaiDeTai.Text.ToString() ;
                detai.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                detai.namhoc = Convert.ToInt16(txtNamHoc.Text);
                detai.hocki = Convert.ToInt16(txtHocKy.Text);
                detai.sotietchuan = Convert.ToInt16(txtSoTietChuan.Text);

                detai.sua_DeTai(detai);
                detai.hienthi_DeTai(dtgDeTai);

                txtHocKy.Text = "";
                txtMaDeTai.Text = "DT";
                txtNamHoc.Text = DateTime.Now.Year.ToString();
                txtSoTietChuan.Text = "";
                txtTenDeTai.Text = "";
                cbGiaoVien.Items.Clear();
                cbLoaiDeTai.Items.Clear();

                loaidetai_Data.loaidetai_Combobox(cbLoaiDeTai);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaDeTai.Text == "")
            {
                MessageBox.Show("Ban chua them ma De Tai");
                txtMaDeTai.Focus();
                return;
            }
            else if (detai_Data.kiemtraMaDT_NXDT(txtMaDeTai.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa De Tai Trong Nhan Xet De Tai");
                return;
            }
            else
            {
                detai.xoa_DeTai(txtMaDeTai.Text.ToString());
                detai.hienthi_DeTai(dtgDeTai);

                txtHocKy.Text = "";
                txtMaDeTai.Text = "DT";
                txtNamHoc.Text = DateTime.Now.Year.ToString();
                txtSoTietChuan.Text = "";
                txtTenDeTai.Text = "";
                cbGiaoVien.Items.Clear();
                cbLoaiDeTai.Items.Clear();

                loaidetai_Data.loaidetai_Combobox(cbLoaiDeTai);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }
        }

        private void dtgDeTai_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgDeTai.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaDeTai.Text = dtgDeTai.Rows[dong].Cells[0].Value.ToString();
            txtTenDeTai.Text = dtgDeTai.Rows[dong].Cells[1].Value.ToString();
            txtHocKy.Text = dtgDeTai.Rows[dong].Cells[4].Value.ToString();
            txtNamHoc.Text = dtgDeTai.Rows[dong].Cells[5].Value.ToString();
            txtSoTietChuan.Text = dtgDeTai.Rows[dong].Cells[6].Value.ToString();

            String loaidetai = dtgDeTai.Rows[dong].Cells[3].Value.ToString();
            detai_Data.layten_LoaiDeTai(loaidetai,cbLoaiDeTai);
            String magiaovien = dtgDeTai.Rows[dong].Cells[2].Value.ToString();
            detai_Data.layten_GiaoVien(magiaovien, cbGiaoVien);
        }

        private void cbLoaiDeTai_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sotinchi = loaidetai_Data.tinchi_DeTai(cbLoaiDeTai.Text) * thamso.tietchuan_tinchi();
            txtSoTietChuan.Text = sotinchi.ToString();
        }

        private void cbGiaoVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            String magiaovien = ((ListItem)(cbGiaoVien.SelectedItem)).value;
            giaovien_Data.thongtin_GiaoVien(dtgGiaoVien, magiaovien);
        }

        private void txtMaDeTai_MouseClick(object sender, MouseEventArgs e)
        {
            txtHocKy.Text = "";
            txtMaDeTai.Text = "DT";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtSoTietChuan.Text = "";
            txtTenDeTai.Text = "";
        }

        private void rdTenDeTai_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTDT.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenDeTai.Checked)
            {
                txtTGV.Visible = false;
                txtTDT.Visible = true;
                txtTDT.Text = "";
            }
        }

        private void rdTenGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTDT.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenDeTai.Checked)
            {
                txtTGV.Visible = false;
                txtTDT.Visible = true;
                txtTDT.Text = "";
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdTenDeTai.Checked)
            {
                detai_Data.timkiemtheo_TenDeTai(dtgDeTai, txtTDT.Text.ToString());
                txtTDT.Text = "";
            }
            else
            {
                detai_Data.timkiemtheo_TenGV(dtgDeTai, txtTGV.Text.ToString());
                txtTGV.Text = "";
            }
        }

        
    }
}
